package com.api.quartz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.api.devices.domain.query.IpcQuery;
import com.api.devices.domain.vo.ElementVo;
import com.api.devices.domain.vo.IpcVo;
import com.api.devices.domain.vo.MonitoredVo;
import com.api.devices.mapper.IpcMapper;
import com.api.quartz.domain.query.CRTabName;
import com.api.quartz.mapper.TaskMapper;
import com.api.quartz.service.ITaskService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TaskServiceImpl implements ITaskService {
    @Autowired
    TaskMapper taskMapper;

    @Autowired
    IpcMapper ipcMapper;

    @Override
    public void refreshTables() {
        List<CRTabName> list = taskMapper.searchCRTabName();
        Set<String> ipcSet = new HashSet<>();
//        Map<String, JSONObject> elementCodeMap = new HashMap();
        List<MonitoredVo> monitoredVoList = new ArrayList<>();
        for (CRTabName item : list) {
            String tableName = item.getTableName();
            String key = "";
            JSONObject jo = new JSONObject();
            if (tableName.contains("resource")) {
                System.out.println("contains resource tableName: "+tableName);
                ipcSet.add(tableName.replace("resource",""));
            } else if (tableName.contains("tracing_serialization")
                    || tableName.contains("tracing_intergration")
                    || tableName.contains("tracing_evaluation")) {
                String monitoredCode = tableName.replace("_tracing_serialization","").replace("_tracing_intergration","").replace("_tracing_evaluation","");
                MonitoredVo vo = new MonitoredVo();
                vo.setMonitoredCode(monitoredCode);
                vo.setIpcCode(monitoredCode.substring(0,7));
                monitoredVoList.add(vo);
//                }

            } else {
                System.out.println("not contains tableName: "+tableName);
            }
//            000_002_resource
//            000_002_001_001_001_tracing_serialization
//            000_002_001_001_001_tracing_intergration
//            000_002_001_001_001_tracing_evaluation
        }
        if (ObjectUtils.isNotEmpty(ipcSet)) {
            insertOrUpIpcs(ipcSet);
        }
        if(ObjectUtils.isNotEmpty(monitoredVoList)){
            insertOrUpMonitored(monitoredVoList);
        }
    }

    private void insertOrUpIpcs(Set<String> ipcSet) {
        JSONObject defIpcName = new JSONObject();
        defIpcName.put("zh-CN", "DefIpcName");
        for (String ipcCode : ipcSet) {
            IpcVo vo = new IpcVo();
            vo.setIpcNameLocales(defIpcName);
            vo.setIpcCode(ipcCode);
            try {
                taskMapper.insertOrUpIpc(vo);
            } catch (Exception e) {
            };
        }
    }

    private void insertOrUpMonitored(List<MonitoredVo> monitoredVoList){
        for(MonitoredVo vo: monitoredVoList){
            taskMapper.insertOrUpMonitored(vo);
        }
    }
//
//    private void insertOrUpElements(Map<String, JSONObject> elementCodeMap) {
//        JSONObject defElemName = new JSONObject();
//        defElemName.put("zh-CN", "");
//        for (String key : elementCodeMap.keySet()) {
//            try {
//                JSONObject jo = elementCodeMap.get(key);
//                Integer adSlot = jo.getInteger("adSlot");
//                Integer intElementCode = jo.getInteger("elementCode");
//                String ipcCode = jo.getString("ipcCode");
////                String elemCode = "00" + intElementCode;
////                elemCode = elemCode.substring(elemCode.length() - 3);
////                IpcVo ipcVo = new IpcVo();
////                ipcVo.setIpcCode(ipcCode);
////                List<IpcQuery> query = ipcMapper.list(ipcVo);
////                String ipcCode = query.get(0).getIpcCode();
////                if (ObjectUtils.isNotEmpty(taskMapper.searchElemQuery(ipcCode, elemCode))) {
////                    ElementVo vo = new ElementVo();
////                    vo.setElementNameLocales(defElemName);
////                    vo.setElementCode(elemCode);
////                    vo.setRobotId(0L);
////                    vo.setIpcId(ipcId);
////                    vo.setIpcSlot(adSlot);
////                    taskMapper.insertOrUpElement(vo);
////                }
//            } catch (Exception e) {}
//        }
//    }
    public void refreshData(){

    }
    public void insertOrUpSerialization(){

    }
    public void insertOrUpIntergration(){

    }
    public void insertOrUpEvaluation(){

    }
}
